/*
 * Copyright (c) 2008-2013 Haulmont. All rights reserved.
 * Use is subject to license terms, see http://www.cuba-platform.com/license for details.
 */

package com.haulmont.asu.web.ui.cathedra_resume;

import com.haulmont.asu.entity.CathedraResume;
import com.haulmont.asu.entity.DiplomaStudent;
import com.haulmont.cuba.gui.components.AbstractEditor;
import com.haulmont.cuba.gui.components.LookupField;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.data.impl.DsListenerAdapter;

import javax.annotation.Nullable;
import javax.inject.Inject;
import java.util.Map;
import java.util.UUID;

/**
 * Author: Sergey42
 * Date: 30.11.13 20:09
 */
public class CathedraResumeEditor extends AbstractEditor<CathedraResume> {

    @Inject
    protected CollectionDatasource<DiplomaStudent, UUID> diplomaStudentDs;

    @Inject
    protected LookupField themeLF;

    @Override
    public void init(Map<String, Object> params) {
        diplomaStudentDs.addListener(new DsListenerAdapter<DiplomaStudent>() {
            @Override
            public void itemChanged(Datasource<DiplomaStudent> ds, @Nullable DiplomaStudent prevItem, @Nullable DiplomaStudent item) {
                themeLF.setEditable(true);
                if (item != null && item.getDiplomaProject() != null) {
                    getItem().setProjectTheme(item.getDiplomaProject().getProjectTheme());
                } else {
                    getItem().setProjectTheme(null);
                }
                themeLF.setEditable(false);
            }
        });
    }
}
